package controller

import (
	"blog_server/global"
	"blog_server/internal/model/request"
	"blog_server/internal/service"
	"blog_server/pkg/app"
	"blog_server/pkg/errcode"
	"github.com/gin-gonic/gin"
	"strconv"
)

type FriendLinkController struct{}

func NewFriendLinkController() FriendLinkController {
	return FriendLinkController{}
}

func (flc *FriendLinkController) GetByPagination(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	pageSize, _ := app.GetPageSize(c)
	page, _ := app.GetPage(c)
	err, res, cnt := svc.GetAllFriendChain(page, pageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetAllCategory err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res, "cnt": cnt})
}

func (flc *FriendLinkController) Create(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	param := request.FriendLinkRequest{}
	err := c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.CreateFriendLink(&param)
	if err != nil {
		global.Logger.Errorf("svc.CreateFriendLink errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
	return
}

func (flc FriendLinkController) Update(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	param := request.FriendLinkRequest{}
	res := c.Param("id")
	linkId, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.UpdateFriendLink(uint(linkId), &param)
	if err != nil {
		global.Logger.Errorf("UpdateFriendLink errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (flc FriendLinkController) Delete(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	res := c.Param("id")
	linkId, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.DeleteFriendLink(uint(linkId))
	if err != nil {
		global.Logger.Errorf("DeleteFriendLink errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (flc FriendLinkController) GetBySearchText(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	searchText := c.Param("search_text")
	res, err := svc.GetFriendLinkListBySearchText(searchText)
	if err != nil {
		global.Logger.Errorf("GetFriendLinkListBySearchText errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res})
}
